//
//  SettingPassWordViewController.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/21.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "SettingPassWordViewController.h"
#import "SettingPaymentPwdViewController.h"
#import "LoginPwdChangeViewController.h"
#import "ChangePaymentPwdViewController.h"

#import "SettingPwdCell.h"

@interface SettingPassWordViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation SettingPassWordViewController

#pragma mark - VC Life

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = @[@"修改登录密码", [[CBTLocalUserManager sharedInstance].status intValue] == 1 ? @"修改支付密码" : @"支付密码设置"];
    
    [self.view addSubview:self.tableView];
    self.navigationItem.title = kLocalizedString(@"密码设置", nil);
}

#pragma mark - ChangeLanguageNotification

- (void)changeLanguage
{
    self.navigationItem.title = kLocalizedString(@"密码设置", nil);
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettingPwdCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.indexRow = indexPath.row;
    cell.backgroundColor = [UIColor whiteColor];
    cell.titleText = self.dataArray[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        LoginPwdChangeViewController *registerVC = [story instantiateViewControllerWithIdentifier:@"loginpwdchangeID"];
        [self.navigationController pushViewController:registerVC animated:YES];
    }else if (indexPath.row == 1){
        @weak(self)
        if ([[CBTLocalUserManager sharedInstance].status intValue] == 1) {
            UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
            UIViewController *registerVC = [story instantiateViewControllerWithIdentifier:@"ChangePaymentPwdID"];
            [self.navigationController pushViewController:registerVC animated:YES];
        }else{
            UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
            SettingPaymentPwdViewController *registerVC = [story instantiateViewControllerWithIdentifier:@"SettingPaymentID"];
            [registerVC setPayMentVCBlock:^{
                self_weak_.dataArray = @[@"修改登录密码", [NSString isBlankString:[CBTLocalUserManager sharedInstance].payPassword] ?  @"支付密码设置" : @"修改支付密码"];
                [self_weak_.tableView reloadData];
            }];
            [self.navigationController pushViewController:registerVC animated:YES];
        }
    }
}

#pragma mark - Lazy

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenNarrowSide, ScreenWideSide - self.tabBarController.tabBar.bounds.size.height - 15) style:(UITableViewStylePlain)];
        _tableView.backgroundColor = [UIColor clearColor];
//        _tableView.contentInset = UIEdgeInsetsMake(5, 0, 0, 0);
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellEditingStyleNone;
        
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        
        if (@available(iOS 11.0, *)){
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _tableView.estimatedRowHeight           = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        
        [_tableView registerClass:[SettingPwdCell class] forCellReuseIdentifier:@"Cell"];
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

@end
